//
//  LYUserDefaults.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/19.
//

import Foundation

let userSessionIdKey = "userSessionId"
let countryIdKey = "countryId"
let phoneNumKey = "phoneNum"
var addressKey: String {
    if appCountryEnv == .indonesia {
        return "address" + "yinni"
    } else {
        return "address" + "yindu"
    }
}
let latitudeKey = "latitudeKey"
let longitudeKey = "longitudeKey"

let loginGoogleMarketKey = "loginGoogleMarketKey"
let homeGoogleMarketKey = "homeGoogleMarketKey"

let loginStartTimeKey = "loginStartTimeKey"
let loginEndTimeKey = "loginEndTimeKey"

let allowNetworkKey = "allowNetworkKey"

class LYUserDefaults {
    private let userDefaults: UserDefaults
    
    // 单例模式
    static let shared = LYUserDefaults()
    
    // 私有初始化方法
    private init() {
        self.userDefaults = UserDefaults.standard
    }
    
    // 自定义初始化（如果需要使用suiteName）
    init(suiteName: String) {
        self.userDefaults = UserDefaults(suiteName: suiteName)!
    }
    
    // MARK: - 基本数据类型操作
    
    /// 存储值
    func set<T>(_ value: T, forKey key: String) {
        userDefaults.set(value, forKey: key)
        userDefaults.synchronize()
    }
    
    /// 获取值（带默认值）
    func get<T>(forKey key: String, defaultValue: T) -> T {
        return userDefaults.object(forKey: key) as? T ?? defaultValue
    }
    
    /// 获取可选值
    func get<T>(forKey key: String) -> T? {
        return userDefaults.object(forKey: key) as? T
    }
    
    /// 删除值
    func remove(forKey key: String) {
        userDefaults.removeObject(forKey: key)
        userDefaults.synchronize()
    }
    
    // MARK: - 类型安全方法
    
    func setString(_ value: String, forKey key: String) {
        set(value, forKey: key)
    }
    
    func getString(forKey key: String) -> String? {
        return get(forKey: key)
    }
    
    func getString(forKey key: String, defaultValue: String) -> String {
        return get(forKey: key, defaultValue: defaultValue)
    }
    
    func setInt(_ value: Int, forKey key: String) {
        set(value, forKey: key)
    }
    
    func getInt(forKey key: String) -> Int? {
        return get(forKey: key)
    }
    
    func getInt(forKey key: String, defaultValue: Int) -> Int {
        return get(forKey: key, defaultValue: defaultValue)
    }
    
    func setBool(_ value: Bool, forKey key: String) {
        set(value, forKey: key)
    }
    
    func getBool(forKey key: String) -> Bool? {
        return get(forKey: key)
    }
    
    func getBool(forKey key: String, defaultValue: Bool) -> Bool {
        return get(forKey: key, defaultValue: defaultValue)
    }
    
    func setDouble(_ value: Double, forKey key: String) {
        set(value, forKey: key)
    }
    
    func getDouble(forKey key: String) -> Double? {
        return get(forKey: key)
    }
    
    func getDouble(forKey key: String, defaultValue: Double) -> Double {
        return get(forKey: key, defaultValue: defaultValue)
    }
    
    func setFloat(_ value: Float, forKey key: String) {
        set(value, forKey: key)
    }
    
    func getFloat(forKey key: String) -> Float? {
        return get(forKey: key)
    }
    
    func getFloat(forKey key: String, defaultValue: Float) -> Float {
        return get(forKey: key, defaultValue: defaultValue)
    }
    
    // MARK: - 复杂对象
    
    func setObject<T: Encodable>(_ value: T, forKey key: String) {
        if let encoded = try? JSONEncoder().encode(value) {
            userDefaults.set(encoded, forKey: key)
            userDefaults.synchronize()
        }
    }
    
    func getObject<T: Decodable>(forKey key: String, type: T.Type) -> T? {
        if let data = userDefaults.data(forKey: key) {
            return try? JSONDecoder().decode(type, from: data)
        }
        return nil
    }
    
    // MARK: - 其他实用方法
    
    /// 检查key是否存在
    func hasValue(forKey key: String) -> Bool {
        return userDefaults.object(forKey: key) != nil
    }
    
    /// 清除所有存储的数据（当前suite）
    func clearAll() {
        if let bundleID = Bundle.main.bundleIdentifier {
            userDefaults.removePersistentDomain(forName: bundleID)
            userDefaults.synchronize()
        }
    }
    
    /// 清除指定keys的数据
    func clear(keys: [String]) {
        keys.forEach { userDefaults.removeObject(forKey: $0) }
        userDefaults.synchronize()
    }
}

